package sqlsource;/*
 * @auther 顶风少年
 * @mail dfsn19970313@foxmail.com
 * @date 2020-01-08 14:31
 * @notify  解决重用性 RawSqlSource 和 DynamicSqlSource 都需要对  #{} 进行替换 ？
 * @version 1.0
 */

import mapping.DynamicContext;
import sqlsource.impl.StaticSqlSource;
import tokenparser.GenericTokenParser;
import tokenparser.impl.ParameterMappingTokenHandler;

public class SqlSourceParser {
    private DynamicContext dynamicContext = null;

    public SqlSourceParser(DynamicContext context) {
        this.dynamicContext = context;
    }

    public SqlSource parse() throws Exception {
        //将 #{ } 替换成 ？ 然后将内容存储到 ParameterMapping 中
        ParameterMappingTokenHandler parameterMappingTokenHandler = new ParameterMappingTokenHandler();
        GenericTokenParser tokenParser = new GenericTokenParser("#{", "}", parameterMappingTokenHandler);
        String sql = tokenParser.parse(dynamicContext.getSql());
        return new StaticSqlSource(sql, parameterMappingTokenHandler.getParameterMappings());
    }
}
